﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;


public class UIManager {
    
    /// 单例模式的核心
    /// 1.定义一个静态的对象，在外界访问，在内部构造
    /// 2.构造方法私有化
    
    private static UIManager _instance;

    public static UIManager Instance {
        get
        {
            if (_instance==null)
            {
                _instance = new UIManager();

            }
            return _instance;
        }
    }
    private Transform canvasTransform;
    private Transform CanvasTransform
    {
        get
        {
            if (canvasTransform==null)
            {
                canvasTransform = GameObject.Find("Canvas").transform;
            }
            return canvasTransform;
        }
    }
    private Dictionary<UIPanelType, string> panelPathDict;//存储所有的面板Prefab的路径
    private Dictionary<UIPanelType, BasePanel> panelDict;//保存所有面板的游戏物体身上的BasePanel组件
    private Stack<BasePanel> panelStack;

    
    private UIManager()
    {
        ParseUIPanelTypeJson();

    }
    /// <summary>
    /// 页面入栈，把页面从界面上显示出来
    /// </summary>
    public void PushPanel(UIPanelType panelType)
    {
        if (panelStack==null)
        {
            panelStack = new Stack<BasePanel>();
        }
        //判断一下栈里面是否有页面
        if (panelStack.Count>0)
            {
                BasePanel topPanel = panelStack.Peek();
                topPanel.OnPause();

            }

            BasePanel panel = GetPanel(panelType);
            panel.OnEnter();
            panelStack.Push(panel);

        
        

    }
    /// <summary>
    /// 页面出栈，把页面从界面上移除
    /// </summary>
    public void PopPanel()
    {
        if (panelStack==null)
        {
            panelStack = new Stack<BasePanel>();

        }
        if (panelStack.Count <= 0) return;

        //关闭栈顶页面的显示
        BasePanel topPanel = panelStack.Pop();
        topPanel.OnExit();

        if (panelStack.Count <= 0) return;
        BasePanel topPanel2 = panelStack.Peek();
        topPanel2.OnResume()
;
}


    /// <summary>
    /// 根据面板类型，得到实例化的面板
    /// </summary>
    public BasePanel GetPanel(UIPanelType panelType)
    {
        if (panelDict == null)
        {
            panelDict = new Dictionary<UIPanelType, BasePanel>();
        }

        
        BasePanel panel = panelDict.TryGet(panelType);

        if (panel == null)
        {
            //如果找不到，那么就找这个面板的prefab的路径，然后去根据prefab去实例化面板
            //string path;
            //panelPathDict.TryGetValue(panelType, out path);
            string path = panelPathDict.TryGet(panelType);
            GameObject instPanel = GameObject.Instantiate(Resources.Load(path)) as GameObject;
            instPanel.transform.SetParent(CanvasTransform, false);
            panelDict.Add(panelType, instPanel.GetComponent<BasePanel>());
            return instPanel.GetComponent<BasePanel>();
        }
        else
        {
            return panel;
        }

    }

    [Serializable]
    /* “持久化”意味着对象的“生存时间”并不取决于程序是否正在执行——它存在或“生存”于程序的每一次调用之间。
    通过序列化一个对象，将其写入磁盘，以后在程序再次调用时重新恢复那个对象，就能圆满实现一种“持久”效果。*/
    class UIPanelTypeJson
    {
        public List<UIPanelInfo> infoList;
    }
     /*解析JSon语句 */
    private void ParseUIPanelTypeJson()
    {
        panelPathDict = new Dictionary<UIPanelType, string>();

        TextAsset ta = Resources.Load<TextAsset>("UIPanelType");

        UIPanelTypeJson jsonObject = JsonUtility.FromJson<UIPanelTypeJson>(ta.text);

        foreach (UIPanelInfo info in jsonObject.infoList)//遍历Json内的数据，将面板地址加到字典中
        {
            //Debug.Log(info.panelType);
            panelPathDict.Add(info.panelType, info.path);
        }
    }
    
}
